package com.kyleswebspace.propertyanalyzer.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.kyleswebspace.parser.FieldParser;
import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

public class LeftMainPanel extends JPanel implements Updateable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7294659523594260871L;
	
	private DataModel dm;
	
	private JTextArea notesTextArea;
	private ImageIcon logo;
	private JLabel disclaimerLabel;
	private final Color backgroundColor = ColorBox.backgroundPanelColor;
	private Font appFont = ArcaFont.appFont;
	
	public LeftMainPanel(DataModel dm, FocusMediator fm) {
		this.dm = dm;
		dm.register(this);
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.NORTHEAST;
		constraints.insets = new Insets(10, 10, 0, 10);
		constraints.ipadx = 20;
		constraints.ipady = 0;
		//constraints.fill = GridBagConstraints.BOTH;

		this.setBackground(backgroundColor);
		
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 10;
		this.add(new PropertyInfoPanel(dm, fm), constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 10;
		constraints.gridheight = 8;
		this.add(new PurchaseCostsPanel(dm, fm), constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 18;
		constraints.gridheight = 6;
		this.add(new CashRequirementsPanel(dm, fm), constraints);

		//notes text area
		//notesTextArea = new JTextArea(4,23);
		
		notesTextArea = new JTextArea();
		//notesTextArea.setPreferredSize(new Dimension(225,50));
		JScrollPane pane = new JScrollPane(notesTextArea);
		pane.setPreferredSize(new Dimension(225,50));
		
		notesTextArea.addFocusListener(this);
		notesTextArea.setLineWrap(true);
		notesTextArea.setWrapStyleWord(true);
		notesTextArea.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 24;
		this.add(pane, constraints);
		
		//notes text area
		disclaimerLabel = new JLabel("<html><i>DISCLAIMER: Please consult a qualified <br/>" +
				"professional before making any <br/>" + 
				"investment decisions. ARCA Real Estate <br/>" +  
				"Investments assumes no responsibility <br/>" + 
				"for anything in this worksheet and do <br/>" +  
				"not guarantee its accuracy. Use this <br/>" +  
				"tool at your own risk.</i></html>");
		disclaimerLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 30;
		constraints.gridheight = 5;
		constraints.anchor = GridBagConstraints.CENTER;
		this.add(disclaimerLabel, constraints);
		
		logo = new ImageIcon(getClass().getResource("arcaLogo.gif"));
		constraints.gridx = 0;
		constraints.gridy = 35;
		this.add(new JLabel(logo), constraints);
		constraints.anchor = GridBagConstraints.EAST;
		
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.gridheight = 10;
		this.add(new IncomePanel(dm, fm), constraints);
		
		constraints.gridx = 1;
		constraints.gridy = 10;
		constraints.gridheight = 31;
		constraints.anchor = GridBagConstraints.NORTH;
		this.add(new ExpensesPanel(dm, fm), constraints);
		
		updateFields();
	}
	
	public void updateFields() {
		this.notesTextArea.setText(dm.getNotes());
	}

	@Override
	public void focusGained(FocusEvent e) {
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		dm.setNotes(this.notesTextArea.getText());
		
		dm.updateInterface();
	}
}
